package com.o2opuzi.lib.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.StringUtils;


/**
 * 
 * @author yue han jiang
 * @date 2015年3月12日
 * @file_name DateUtils.java
 */
public class DateUtils {

	private static Log log = LogFactory.getLog(DateUtils.class);

	private static String defaultDatePattern = "yyyy-MM-dd HH-mm-ss";

	/**
	 * 获得默认的 date pattern
	 */
	public static String getDatePattern() {
		return defaultDatePattern;
	}

	/**
	 * 返回预设Format的当前日期字符串
	 */
	public static String getToday() {
		Date today = new Date();
		return format(today);
	}

	/**
	 * 获取开始时间
	 * 
	 * @author wang yi wang765aaa@163.com
	 * @param tradeDate
	 * @return
	 */
	public static Date getBeginTime(String tradeDate) {
		Date beginTime = null;
		if (!StringUtils.isEmpty(tradeDate)) {
			String[] split = tradeDate.split(" -");
			beginTime = DateUtils.parse(split[0], "yyyy/MM/dd");
		}
		return beginTime;
	}

	/**
	 * 获取结束时间
	 * 
	 * @author wang yi wang765aaa@163.com
	 * @param tradeDate
	 * @return
	 */
	public static Date getEndTime(String tradeDate) {
		Date endTime = null;
		if (!StringUtils.isEmpty(tradeDate)) {
			String[] split = tradeDate.split(" -");
			endTime = DateUtils.parse(split[1], "yyyy/MM/dd");
		}
		return endTime;
	}

	/**
	 * 使用预设Format格式化Date成字符串
	 */
	public static String format(Date date) {
		return date == null ? "" : format(date, getDatePattern());
	}

	/**
	 * 使用参数Format格式化Date成字符串
	 */
	public static String format(Date date, String pattern) {
		return date == null ? "" : new SimpleDateFormat(pattern).format(date);
	}

	/**
	 * 使用预设格式将字符串转为Date
	 */
	public static Date parse(String strDate) {
		return StringUtils.isEmpty(strDate) ? null : parse(strDate, getDatePattern());
	}

	/**
	 * 使用参数Format将字符串转为Date
	 */
	public static Date parse(String strDate, String pattern) {
		try {
			return StringUtils.isEmpty(strDate) ? null : new SimpleDateFormat(pattern).parse(strDate);
		} catch (ParseException e) {
			log.error(e);
			return null;
		}
	}

	/**
	 * 在日期上增加数个整月
	 */
	public static Date addMonth(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, n);
		return cal.getTime();
	}

	/**
	 * 在日期上增加数个整天
	 */
	public static Date addDay(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, n);
		return cal.getTime();
	}

	/**
	 * 根据日期获得该日的周一
	 * 
	 * @param date
	 * @return 该日的周一
	 */
	public static String getFirstDayOfWeek(Date date) {
		Calendar c = new GregorianCalendar();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setTime(date);
		c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
		return format(c.getTime(), "yyyyMMdd");
	}

	/**
	 * 根据日期获得该日的周二
	 * 
	 * @param date
	 * @return 该周的周二
	 */
	public static String getLastDayOfTuesday(Date date) {
		Calendar c = new GregorianCalendar();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setTime(date);
		c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 1); // Sunday
		return format(c.getTime(), "yyyyMMdd");
	}

	/**
	 * 根据日期获得该日的周三
	 * 
	 * 
	 * @param date
	 * @return 该周的周三
	 */
	public static String getLastDayOfWednesday(Date date) {
		Calendar c = new GregorianCalendar();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setTime(date);
		c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 2); // Sunday
		return format(c.getTime(), "yyyyMMdd");
	}

	/**
	 * 根据日期获得该日的周四
	 * 
	 * 
	 * @param date
	 * @return 该周的周四
	 */
	public static String getLastDayOfTursday(Date date) {
		Calendar c = new GregorianCalendar();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setTime(date);
		c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 3); // Sunday
		return format(c.getTime(), "yyyyMMdd");
	}

	/**
	 * 根据日期获得该日的周五
	 * 
	 * 
	 * @param date
	 * @return 该周的周五
	 */
	public static String getLastDayOfFriday(Date date) {
		Calendar c = new GregorianCalendar();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setTime(date);
		c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 4); // Sunday
		return format(c.getTime(), "yyyyMMdd");
	}

	/**
	 * 根据日期获得该日的周六
	 * 
	 * 
	 * @param date
	 * @return 该周的周六
	 */
	public static String getLastDayOfSaterday(Date date) {
		Calendar c = new GregorianCalendar();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setTime(date);
		c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 5); // Sunday
		return format(c.getTime(), "yyyyMMdd");
	}

	/**
	 * 根据日期获得该日的周日
	 * 
	 * 
	 * @param date
	 * @return 该日的周日
	 */
	public static String getLastDayOfWeek(Date date) {
		Calendar c = new GregorianCalendar();
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setTime(date);
		c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
		return format(c.getTime(), "yyyyMMdd");
	}

	/**
	 * 根据指定参数,返回当前日期的上周,或者下周(1,为下周,-1为上周,以此类推)
	 * 
	 * @param week
	 * @return 该周的周一和周五的日期
	 */

	public static Map<String, String> GetDay(int week) {
		Map<String, String> map = new HashMap<String, String>();
		// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
		Calendar calendar = Calendar.getInstance();
		calendar.add(5, week * 7);
		map.put("startMonday", getFirstDayOfWeek(calendar.getTime()));
		map.put("tuesday", getLastDayOfTuesday(calendar.getTime()));
		map.put("wednesday", getLastDayOfWednesday(calendar.getTime()));
		map.put("turesday", getLastDayOfTursday(calendar.getTime()));
		map.put("friday", getLastDayOfFriday(calendar.getTime()));
		map.put("saterday", getLastDayOfSaterday(calendar.getTime()));
		map.put("endSunday", getLastDayOfWeek(calendar.getTime()));
		return map;
	}

	/**
	 * 指定周数，返回当前周的周一，周日是的日期
	 * 
	 * @param week
	 *            周数
	 * @return 挂号资源列表
	 */
	public static String getWeekForDate(int week) {
		Map<String, String> map = DateUtils.GetDay(week);
		String firstDay = map.get("startMonday");
		String tuesday = map.get("tuesday");
		String wednesday = map.get("wednesday");
		String turesday = map.get("turesday");
		String friday = map.get("friday");
		String saterday = map.get("saterday");
		String lastDay = map.get("endSunday");
		return firstDay + "," + tuesday + "," + wednesday + "," + turesday + "," + friday + "," + saterday + ","
				+ lastDay;
	}

	public static String getDate(String pattern) {
		return new SimpleDateFormat(pattern).format(new Date());
	}

	public static Date getDate(Date date, String pattern) {
		return DateUtils.parse(new SimpleDateFormat(pattern).format(date), pattern);
	}

	public static Date parseUTCTime(String date) {
		return new Date(Long.valueOf(Long.parseLong(date)));
	}

	/**
	 * @Description 获取最近几天
	 * @param d
	 * @param day
	 * @return
	 * @Return_type Date
	 * @Date 2016年2月3日 上午10:08:21
	 * @Author Jiangsh
	 */
	public static Date getDateBefore(Date d, int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
		return now.getTime();
	}

	public static int[] getTimeSevenDays() {
		Date d = new Date();
		int[] nums = new int[7];
		for (int i = 1; i <= 7; i++) {
			Date date = DateUtils.getDateBefore(d, i);
			String datestr = DateUtils.format(date, "yyyy-MM-dd");
			nums[i - 1] = Integer.parseInt(datestr.substring(8, 10));
		}
		return nums;
	}

	/**
	 * 
	 * @Description:返回的格式为 “月-日期”，day为最近多少天
	 * @param day
	 * @return String
	 * @exception:
	 * @author: xiang jia ming
	 * @time:2016年3月4日 下午4:41:40
	 */
	public static String getTimeDays(int day) {
		Date d = new Date();
		String str = "";
		for (int i = 0; i < day;) {
			Date date = DateUtils.getDateBefore(d, i);
			String datestr = DateUtils.format(date, "yyyy-MM-dd");
			str += (date.getMonth() + 1) + "-" + Integer.parseInt(datestr.substring(8, 10));
			System.err.println(datestr);
			if (i < day - 1) {
				str += ",";
			}
			if (day == 30) {
				if (i == 0 || i == 29) {
					i += 4;
				} else {
					i += 5;
				}
			} else {
				i++;
			}
		}
		return str;
	}

	/**
	 * @Description:根据天数返回list集合
	 * @param day
	 * @return List<Object>
	 * @exception:
	 * @author: wang yi
	 * @time:2016年3月4日 下午5:02:45
	 */
	public static List<String> getTimesDays(int day) {
		Date d = new Date();
		String str = "";
		List<String> list = new ArrayList<>();
		for (int i = 0; i < day;) {
			Date date = DateUtils.getDateBefore(d, i);
			String datestr = DateUtils.format(date, "yyyy-MM-dd");
			list.add(datestr);
			System.err.println("=="+datestr);
			str += (date.getYear() + date.getMonth() + 1) + Integer.parseInt(datestr.substring(8, 10));
			if (i < day - 1) {
				/* str += ","; */
			/*	list.add(str);*/
			}
			if (day == 30) {
				if (i == 0 || i == 29) {
					i += 4;
				} else {
					i += 5;
				}
			} else {
				i++;
			}
		}
		return list;
	}

	/**
	 * @Description 获取本季度
	 * @return
	 * @Return_type int[]
	 * @Date 2016年2月3日 上午10:09:03
	 * @Author Jiangsh
	 */
	public static int[] getTimeNinetyDays() {
		String todayDate = DateUtils.getToday();
		int month = Integer.parseInt(todayDate.substring(5, 7));
		int[] months = new int[3];
		if (1 <= month && month <= 3) {
			months[0] = 1;
			months[1] = 2;
			months[2] = 3;
		} else if (4 <= month && month <= 6) {
			months[0] = 4;
			months[1] = 5;
			months[2] = 6;
		} else if (7 <= month && month <= 9) {
			months[0] = 7;
			months[1] = 8;
			months[2] = 9;
		} else if (10 <= month && month <= 12) {
			months[0] = 10;
			months[1] = 11;
			months[2] = 12;
		}
		return months;
	}

	public static void main(String[] args) throws Exception {
		// System.out.println(DateUtils.format(new Date(1423238683)));

		/*
		 * Calendar calendar = Calendar.getInstance(); calendar.setTime(new
		 * Date()); calendar.add(Calendar.MONTH,12); Date time =
		 * calendar.getTime();
		 * 
		 * 
		 * System.out.println(DateUtils.format(time));
		 * 
		 */

		Date d1 = new SimpleDateFormat("yyyy-MM").parse("2015-02");

		Date d2 = new SimpleDateFormat("yyyy-MM").parse("2015-03");

		long t = d2.getTime() - d1.getTime();

		int day = (int) (t / (24 * 60 * 60 * 1000));

		System.out.println(day);

	}
}